package com.caiyi.financial.nirvana.sms.service;


import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsUserSign;
import com.caiyi.financial.nirvana.sms.enums.*;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsUserSignMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.SignQueryBean;
import com.youyu.common.api.Result;
import com.github.pagehelper.PageHelper;
import com.youyu.common.utils.YyAssert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SmsUserSignService extends BaseService<SmsUserSign> {


    @Autowired
    private SmsUserSignMapper smsUserSignMapper;

    public PageBean<SmsUserSign> getSignByParams(SignQueryBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsUserSign> list = smsUserSignMapper.selectSignListByParams(bean);
        PageBean<SmsUserSign> pageBean = new PageBean<>(list);
        return pageBean;
    }

    public int updateSign(Integer id, Integer status, String reason) {
        YyAssert.isFalse(status != SmsUserSignStatusEnum.PASS.index && status != SmsUserSignStatusEnum.REJECT.index, ReturnCodeEnum.PARAMS_ERROR.code, "状态只能设置为审核通过、审核驳回");
        SmsUserSign userSign = new SmsUserSign();
        userSign.setId(id);
        userSign = this.selectByPrimaryKey(userSign);
        int updateCount = smsUserSignMapper.updateUserSignById(reason, status, id);
        YyAssert.isFalse(updateCount == 0, ReturnCodeEnum.PARAMS_ERROR.code, "更新失败");
        updateRedis(userSign.getAccount());
        return updateCount;
    }


    /**
     * @param:defauls 默认为1，非默认为0
     * 该签名设置为默认需要更新用户签名列
     */
    public int updateSignDefault(Integer id) {
        YyAssert.isFalse(id == null, ReturnCodeEnum.PARAMS_ERROR.code, "主键不能为空");
        SmsUserSign smsUserSign = new SmsUserSign();
        smsUserSign.setId(id);
        smsUserSign = this.selectByPrimaryKey(smsUserSign);
        smsUserSignMapper.updateSignDefault(smsUserSign.getAccount());
        int updateCount = smsUserSignMapper.updateSignByAccountAndId(id, smsUserSign.getAccount());
        YyAssert.isFalse(updateCount == 0, ReturnCodeEnum.PARAMS_ERROR.code, "设置默认失败");
        updateRedis(smsUserSign.getAccount());
        return updateCount;
    }


    public int deleteSmsSign(Integer id) {
        YyAssert.isFalse(id == null, ReturnCodeEnum.PARAMS_ERROR.code, "ID不能为空");
        SmsUserSign smsUserSign = new SmsUserSign();
        smsUserSign.setId(id);
        smsUserSign = this.selectByPrimaryKey(smsUserSign);
        int deleteCount = smsUserSignMapper.delByAccountAndId(smsUserSign.getAccount(), id);
        YyAssert.isFalse(deleteCount == 0, ReturnCodeEnum.PARAMS_ERROR.code, "删除失败");
        updateRedis(smsUserSign.getAccount());
        logger.info("admin deleteSmsSign id {}, status is {}", id, smsUserSign.getStatus());
        return deleteCount;
    }

    public int addSign(SmsUserSign smsSign) {
        YyAssert.isFalse(Objects.nonNull(smsSign.getReason()) && smsSign.getReason().length() > 50, ReturnCodeEnum.PARAMS_ERROR.code, "审核原因过长");
        YyAssert.isFalse(Objects.isNull(smsSign.getApplication()) || (smsSign.getApplication() != SmsUserSignApplicationEnum.OTHER.index && smsSign.getApplication() != SmsUserSignApplicationEnum.MY.index && smsSign.getApplication() != SmsUserSignApplicationEnum.MYAPP.index), ReturnCodeEnum.PARAMS_ERROR.code, "应用场景只能设置为为自己公司、为自己产品、为他人产品");
        YyAssert.isFalse(Objects.isNull(smsSign.getIndustry()) || (smsSign.getIndustry() != SmsUserSignIndustryEnum.BANKING.index && smsSign.getIndustry() != SmsUserSignIndustryEnum.GAME.index && smsSign.getIndustry() != SmsUserSignIndustryEnum.OTHER.index), ReturnCodeEnum.PARAMS_ERROR.code, "行业只能为网络游戏、互联网金融、其他");
        YyAssert.isFalse(Objects.isNull(smsSign.getDefauls()) || (smsSign.getDefauls() != SmsUserSignDefaultEnum.DEFAULT.index && smsSign.getDefauls() != SmsUserSignDefaultEnum.SETTING.index), ReturnCodeEnum.PARAMS_ERROR.code, "默认值只能设置为0,1");
        YyAssert.isFalse(StringUtils.isBlank(smsSign.getSign()), ReturnCodeEnum.PARAMS_ERROR.code, "签名不能为空");
        YyAssert.isFalse(smsSign.getSign().length() > 50, ReturnCodeEnum.PARAMS_ERROR.code, "签名过长");

        smsSign.setStatus(SmsUserSignStatusEnum.PASS.index);
        if (smsSign.getDefauls() == SmsUserSignDefaultEnum.SETTING.index) {
            smsUserSignMapper.updateSignDefault(smsSign.getAccount());
        }
        //签名前后加上【】
        if (!smsSign.getSign().startsWith("【")) {
            smsSign.setSign("【" + smsSign.getSign());
        }
        if (!smsSign.getSign().endsWith("】")) {
            smsSign.setSign(smsSign.getSign() + "】");
        }
        List<String> list = smsUserSignMapper.selectSignListByAccounts(smsSign.getAccount());
        YyAssert.isFalse(list.contains(smsSign.getSign()), ReturnCodeEnum.PARAMS_ERROR.code, "签名已存在");
        int insertCount = this.insertByEntitySelective(smsSign);
        YyAssert.isFalse(insertCount == 0, ReturnCodeEnum.PARAMS_ERROR.code, "添加失败");
        updateRedis(smsSign.getAccount());
        return insertCount;
    }

    public void updateRedis(String account) {
        String key = String.format(JedisKey.SMS_SIGN, account);
        List<String> list = smsUserSignMapper.selectSignListByAccount(account);
        if (list.size() == 0) {
            JedisService.del(key);
            logger.info("admin updateRedis account is {} sign is null", account);
            return;
        }
        Map<String, Double> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i), (double) list.size() - i - 1);
        }
        JedisService.del(key);
        JedisService.zadd(key, map);
        logger.info("admin updateRedis account is {},addSignCount is {} ", account, map.size());
    }
}
